package com.main;

import java.io.IOException;
import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

import com.util.MyDialog;
import com.util.Tool;

public class Main {
	
	Display display = null;
	Shell shell = null;
	
	Label timeLabel = null;
	Composite rightComposite = null;
	
	Spinner hourSpinner = null;
	Spinner minuteSpinner = null;
	
	final int time = 1000;
	static boolean hasShowDialog = false;
	
	public void run() {
		display = Display.getDefault();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		shell.setImage(new Image(display, Main.class.getResourceAsStream("dy.jpg")));
		shell.setText("不可描述");
		shell.setToolTipText("最小化到托盘吧");
		
		Image image = new Image(display, Main.class.getResourceAsStream("bg.jpg"));
		shell.setBackgroundImage(image);
		
		// 链接
		Link link = new Link(shell, SWT.NONE);
		link.setText("<a>源码下载</a>");
		link.setBounds(100, 60, 100, 20);
		link.addSelectionListener(new SelectionAdapter(){
		    public void widgetSelected(SelectionEvent event){
		        try {
					Runtime.getRuntime().exec( "explorer.exe http://git.ybserver.com/yulifu/gui_douyu" );
				} catch (IOException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
		    }
		});
		
		// 托盘
		this.tuopan();
		
		// 右侧
		this.renderRight(shell);
		
		// 渲染
		shell.pack();
		shell.setLocation(Math.round((Tool.getScreenWidth() - shell.getBounds().width) / 2), 200);
		shell.open();
		
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		display.dispose();
	}
	
	private void tuopan() {
		final Tray tray = display.getSystemTray();
        final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
        
        // 托盘图标
        trayItem.setImage(shell.getImage());
        trayItem.setToolTipText(shell.getText());
        trayItem.setVisible(true);
        
        // 托盘菜单
        final Menu trayMenu = new Menu(shell, SWT.POP_UP);
        
        // 托盘菜单选项
        MenuItem openItem = new MenuItem(trayMenu, SWT.PUSH);
        openItem.setText("显示窗口");
        new MenuItem(trayMenu, SWT.SEPARATOR);
        MenuItem exitItem = new MenuItem(trayMenu, SWT.PUSH);
        exitItem.setText("退出");
        
        
        // 菜单事件
        openItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                shell.setVisible(true);
                shell.setActive();
            }
        });
        exitItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                shell.dispose();
            }
        });
        
        // 显示托盘菜单
        trayItem.addMenuDetectListener(new MenuDetectListener(){
            public void menuDetected(MenuDetectEvent e) {
                trayMenu.setVisible(true);
            }
        });
 
        //注册窗口事件监听器
        shell.addShellListener(new ShellAdapter() {
            // 点击窗口最小化按钮时
            public void shellIconified(ShellEvent e) {
            	e.doit = false;
            	shell.setVisible(false);
            }
 
            // 点击窗口关闭按钮时 并不终止程序 而是隐藏窗口
            //public void shellClosed(ShellEvent e) {
            //    e.doit = false;
            //    shell.setVisible(false);
            //}
        });
	}

	private void renderRight(Shell shell) {
		// 左右容器
		rightComposite = new Composite(shell, SWT.NONE);
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		rightComposite.setLayout(layout);
		rightComposite.setBounds(260, 0, 220, 300);
		
		new DateTime(rightComposite, SWT.CALENDAR);
		
		Group group = new Group(rightComposite, SWT.NONE);
		group.setLayout(new RowLayout());
		group.setText("设置提示时间：");
		
		hourSpinner = new Spinner(group, SWT.BORDER);
		hourSpinner.setMinimum(0);
		hourSpinner.setMaximum(23);
		hourSpinner.setSelection(17);
		hourSpinner.setIncrement(1);
		
		new Label(group, SWT.NONE).setText("点");
		
		minuteSpinner = new Spinner(group, SWT.BORDER);
		minuteSpinner.setMinimum(0);
		minuteSpinner.setMaximum(59);
		minuteSpinner.setSelection(30);
		minuteSpinner.setIncrement(1);
		
		timeLabel = new Label(rightComposite, SWT.BORDER);
		timeLabel.setText("XXXX-XX-XX XX:XX:XX");
		
		this.startTimer();

		// 执行布局
		rightComposite.layout();
	}

	private void startTimer() {
		Runnable timer = new Runnable () {
			@Override
			public void run () {
				if (shell.isDisposed()) return;
				
				Calendar now = Calendar.getInstance();  
				int h = now.get(Calendar.HOUR_OF_DAY);
				int m = now.get(Calendar.MINUTE);
				
				timeLabel.setText(now.get(Calendar.YEAR) + "-"
					+ (now.get(Calendar.MONTH) + 1) + "-"
					+ now.get(Calendar.DAY_OF_MONTH) + " "
					+ h + ":"
					+ m + ":"
					+ now.get(Calendar.SECOND));
				
				if(h == hourSpinner.getSelection() && m == minuteSpinner.getSelection()) {
					if(!Main.hasShowDialog) {
						Main.hasShowDialog = true;
						
						new MyDialog();
					}
					
				} else {
					display.timerExec(time, this);
				}
			}
		};
		
		display.timerExec(time, timer);
	}

	public static void main(String[] args) {
		new Main().run();
	}

}
